<template>
  <view class="main">
    <view v-if="type == 0">
      <view class="title">修改昵称</view>
      <view class="msg">(一周只能修改一次)</view>
      <input class="input-phone" type="text" v-model="nickName" placeholder="请输入昵称" />
      <button type="primary" @click="onChangeUserNick">点击修改</button>
    </view>
    <view v-if="type == 3">
      <view class="title">信息补充</view>
      <view class="" v-if="!phoneFlag">
        <input  class="input-phone" type="text" v-model="phone" placeholder="请输入手机号" />
      </view>
      <view class="" v-else>
        <view calass="temp">手机号：{{phone}}</view>
      </view>
      <view class="" v-if="!emailFlag">
        <input  class="input-phone" type="text" v-model="email" placeholder="请输入邮箱" />
      </view>
      <view class="" v-else>
        <view calass="temp">邮箱：{{email}}</view>
      </view>
      <button type="primary" @click="onAddUserInfo">点击提交</button>
      <button type="primary" @click="onAddUserInfo1">身份认证</button>
    </view>
    <view v-else-if="type == 20">
      <text>提示：{{msg}}</text>
      <input  class="input-phone" type="text" v-model="name" placeholder="姓名" />
      <input  class="input-phone" type="text" v-model="sfz" placeholder="身份证" />
      <button type="primary1" @click="onAddUserInfo3">点击提交</button>
    </view>
    <view v-else-if="type == 2">
      <view class="title">联系客服</view>
      <view class="msg">(点击复制)</view>
      <view style="margin-top: 5%;">微信号：<text @tap="copyText(callService)">{{callService}}</text></view>
    </view>
  </view>
</template>

<script>
  export default {
    name: "settingAndCall",
    data() {
      return {
        type: '',
        uid: '',
        nickName: '',
        callService: '**********',
        user: {},
        phone: '',
        phoneFlag: true,
        email: '',
        emailFlag: true,
        name: '',
        sfz: '',
        msg: "无",
      };
    },
    onLoad(options) {
      console.log(options)
      this.type = options.type;
      this.uid = options.uid;
      console.log(this.type)
      this.getUserInfo(this.uid);
    },
    methods: {
      async onAddUserInfo1(){
        this.type = 20;
        const {
          data: result
        } = await uni.$http.post('/api/READ/api/my/user/get/sfz/' + this.uid);
        console.log(result)
        if(result.code != '200') return uni.$showMsg(result.msg);
        if (result.data != null) {
          this.msg = "认证成功";
        }
      },
      // 点击复制微信号
      copyText(res) {
        uni.setClipboardData({
          data: res,
          success: () => {
            uni.showToast({
              title: '复制成功'
            })
          }
        })
      },
      async onAddUserInfo3(){
        var user = {
          uid: this.uid,
          name:this.name,
          sfz: this.sfz,
        }
        console.log(user);
        const {
          data: result
        } = await uni.$http.post('/api/READ/api/my/user/sfz', user);
        console.log(result)
        if(result.code != '200') return uni.$showMsg(result.msg);
        uni.$showMsg(result.data.respMessage);
        
        const {
          data: result1
        } = await uni.$http.post('/api/READ/api/my/user/get/sfz/' + this.uid);
        console.log(result1)
        if(result1.code != '200') return uni.$showMsg(result1.msg);
        if (result1.data != null) {
          this.msg = "认证成功";
        }
        
      },
      async onAddUserInfo(){
        var user = {
          uid: this.uid,
          email:this.email,
          phone: this.phone,
        }
        console.log(user);
        const {
          data: result
        } = await uni.$http.post('/api/READ/api/my/user/add/info', user);
        console.log(result)
        if(result.code != '200') return uni.$showMsg(result.msg);
        uni.$showMsg("修改成功");
      },
      async getUserInfo(uid){
        const {
          data: result
        } = await uni.$http.post('/api/READ/api/my/user/get/'+uid);
        console.log(result)
        if(result.code != '200') return uni.$showMsg(result.msg);
        var rels = result.data;
        if (rels.email == null) {
          this.emailFlag = false;
        }
        if (rels.phone == null) {
           this.emailFlag = false;
        }
        this.phone = rels.phone;
        this.email = rels.email;
      },
      async onChangeUserNick() {
        if (this.nickName.length < 1) {
          return uni.$showMsg("昵称不能为空"); 
        }
        
        let condition = {
          uid: this.uid,
          nickName : this.nickName
        }
        const {
          data: result
        } = await uni.$http.post('/api/READ/api/my/user/change/nick', condition);
        console.log(result)
        if(result.code != '200') return uni.$showMsg(result.msg);
        uni.$showMsg("修改成功");
      },
    }
  }
</script>

<style lang="scss">
  .temp{
    margin: 25rpx;
  }
  .main {
    margin: 5%;
  }

  .title {
    font-size: 35rpx;
    font-weight: bold;
  }

  .msg {
    font-size: 25rpx;
    margin-top: 4%;
  }

  .input-phone {
    width: 100%;
    height: 100upx;
    border-bottom: 1px solid #ccc;
  }

  button[type="primary"] {
    background-color: #00AA00;
    margin-top: 5%;
    color: #fff;
    width: 200rpx;
    // padding: 10px 20px;
    font-size: 30rpx;
    border: none;
    border-radius: 5px;
    cursor: pointer;
  }
  
  button[type="primary1"] {
    background-color: #00AA00;
    margin-top: 5%;
    color: #fff;
    width: 300rpx;
    // padding: 10px 20px;
    font-size: 30rpx;
    border: none;
    border-radius: 5px;
    cursor: pointer;
  }
</style>
